proc userplace (shiplen : int, shipid : int)
    putout ("Place " + intstr (shiplen) + " unit long ship")
    var startx, starty : int
    loop
	Window.Select (GridWin)
	refreshusergrid (pwn)
	get_square (GridWin, gray)
	startx := userx
	starty := usery
	Window.Select (GridWin)
	if userloc (startx, starty) = 0 then
	    Draw.FillOval (((startx - 1) * 20) + 10, ((starty - 1) * 20) + 10, 10, 10, grey)
	end if
	userships (shipid).startx := startx
	userships (shipid).starty := starty
	get_square (GridWin, gray)
	userships (shipid).endx := userx
	userships (shipid).endy := usery
	Window.Select (GridWin)
	if userloc (userx, usery) = 0 then
	    Draw.FillOval (((userx - 1) * 20) + 10, ((usery - 1) * 20) + 10, 10, 10, grey)
	end if
	var bad := 0
	if userx = startx + (shiplen - 1) then
	    Window.Select (GridWin)
	    for i : startx .. userx
		if userloc (i, starty) = 1 then
		    bad := 1
		end if
	    end for
	    if bad = 0 then
		for i : startx .. userx
		    userloc (i, starty) := 1
		    Draw.FillOval (((i - 1) * 20) + 10, ((starty - 1) * 20) + 10, 10, 10, black)
		    userships (shipid).mode := 1
		end for
	    end if
	elsif userx = startx - (shiplen - 1) then
	    for decreasing i : startx .. userx
		if userloc (i, starty) = 1 then
		    bad := 1
		end if
	    end for
	    if bad = 0 then
		Window.Select (GridWin)
		for decreasing i : startx .. userx
		    userloc (i, starty) := 1
		    %Draw.FillOval (((i - 1) * 20) + 10, ((starty - 1) * 20) + 10, 10, 10, black)
		    userships (shipid).mode := 2
		end for
	    end if
	elsif usery = starty + (shiplen - 1) then
	    for i : starty .. usery
		if userloc (startx, i) = 1 then
		    bad := 1
		end if
	    end for
	    if bad = 0 then
		Window.Select (GridWin)
		for i : starty .. usery
		    userloc (startx, i) := 1
		    Draw.FillOval (((startx - 1) * 20) + 10, ((i - 1) * 20) + 10, 10, 10, black)
		    userships (shipid).mode := 3
		end for
	    end if
	elsif usery = starty - (shiplen - 1) then
	    for decreasing i : starty .. usery
		if userloc (startx, i) = 1 then
		    bad := 1
		end if
	    end for
	    if bad = 0 then
		Window.Select (GridWin)
		for decreasing i : starty .. usery
		    userloc (startx, i) := 1
		    Draw.FillOval (((startx - 1) * 20) + 10, ((i - 1) * 20) + 10, 10, 10, black)
		    userships (shipid).mode := 4
		end for
	    end if
	else
	    bad := 1
	end if
	exit when bad = 0
	putout ("Invalid placement, please try somewhere else!")
    end loop
    refreshusergrid(pwn)
    pwn += 1
end userplace
